package org.example.controller;

import org.example.domain.Food;
import org.example.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodService foodService;

    @PostMapping("/save")
    public Result save(@RequestBody Food food)
    {
        boolean result = foodService.save(food);
        return new Result(result,result ? Code.SAVE_OK:Code.SAVE_ERR);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Food  food){
        boolean result = foodService.update(food);
        return new Result(result,result ? Code.UPDATE_OK:Code.UPDATE_ERR);
    }

    @DeleteMapping("/delete")
    public Result  delete(Integer id){
        boolean result = foodService.delete(id);
        return new Result(result,result ? Code.DELETE_OK:Code.DELETE_ERR);
    }
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Food food=foodService.findById(id);
        Integer code=food != null ? Code.GET_OK:Code.GET_ERR;
        String msg=food != null ? "查询成功" : "查询失败";
        return new Result(food,code,msg);
    }
    @GetMapping("/findAll")
    public Result findAll(){
        List< Food> foodList=foodService.findAll();
        Integer code=foodList != null ? Code.GET_OK:Code.GET_ERR;
        String msg=foodList != null ? "查询成功" : "查询失败";
        System.out.println(foodList);
        return new Result(foodList,code,msg);
    }
}
